
//jinzilong add start 20120328
package com.android.matmanual;


import org.apache.http.util.EncodingUtils;

import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.BatteryManager;
import android.os.Bundle;
import android.os.Handler;
import android.os.PowerManager;
import android.os.PowerManager.WakeLock;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.WindowManager;
import android.widget.LinearLayout;
import android.widget.TextView;

import java.io.FileInputStream;
import java.io.IOException;


public class ChargingTest extends BaseActivity{

	private BatteryReceiver receiver=null;
	IntentFilter mIntentFilter;
	TextView tvCharging;
	TextView quantity;
	TextView bvoltage;
	//TextView cvoltage;
	 // xiongxi add 2012-04-09
    private static final String PATH = "/sys/class/power_supply/battery/current_now";
	//private static final String PATH = "/system/real_time_current";
    TextView mCurrent;
	boolean ischarging = false;
    Handler handler = new Handler();
    Runnable updateCurrent = new Runnable() {

        @Override
        public void run() {
            String currentInfo = getCurrent(PATH);
            int current = 0;
            if(!"discharging".equals(currentInfo.trim()))
            {
                current = Integer.parseInt(currentInfo.trim());
            }
			if(!ischarging)
            {
            	current = 0;
            }
            mCurrent.setText(getResources().getString(R.string.current,current));
            handler.postDelayed(updateCurrent, 1000);
        }
    };
    // end add	
	//TextView temp;
	TextView Status;
	WakeLock mWakeLock;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.chargingtest);
		setTitle(R.string.Charging_name);
	    PowerManager pm =
		           (PowerManager) getSystemService(POWER_SERVICE); 
		    mWakeLock = pm.newWakeLock(PowerManager.SCREEN_BRIGHT_WAKE_LOCK, 
		          "XYTEST"); 
		    mWakeLock.acquire(); 		
		mIntentFilter = new IntentFilter();    
		receiver = new BatteryReceiver();
		mIntentFilter.addAction(Intent.ACTION_BATTERY_CHANGED);
		tvCharging = (TextView)findViewById(R.id.charging);
		quantity = (TextView)findViewById(R.id.quantity);
		bvoltage = (TextView)findViewById(R.id.bvoltage);
		//cvoltage = (TextView)findViewById(R.id.cvoltage);
		mCurrent = (TextView) findViewById(R.id.current);
		//temp = (TextView)findViewById(R.id.temp);
		Status = (TextView)findViewById(R.id.status);
	
		registerReceiver(receiver, mIntentFilter);  
		
	}
	private class BatteryReceiver extends BroadcastReceiver{
		@Override
		public void onReceive(Context arg0, Intent arg1) {
			// TODO Auto-generated method stub
			String action = arg1.getAction();
			int plugged = arg1.getIntExtra("plugged", 0); 
			int current = arg1.getIntExtra("level",0);
			int voltage = arg1.getIntExtra("voltage", 0);
			int temperature = arg1.getIntExtra("temperature", 0);
			int health = arg1.getIntExtra("health", 0);
			int status = arg1.getIntExtra("status", 0);  
			String id="0";
			switch (status) {   
	           case BatteryManager.BATTERY_STATUS_UNKNOWN:   
	               
	               break;   
	           case BatteryManager.BATTERY_STATUS_CHARGING:   
				   ischarging = true;
	        	   id = getResources().getString(R.string.ischarging);
	               break;   
	           case BatteryManager.BATTERY_STATUS_DISCHARGING:  
					ischarging = false;
	        	   id = getResources().getString(R.string.discharging);
	               break;   
	           case BatteryManager.BATTERY_STATUS_NOT_CHARGING:   
			   ischarging = false;
	               id = getResources().getString(R.string.nocharging);
	               break;   
	           case BatteryManager.BATTERY_STATUS_FULL:   
	               id = getResources().getString(R.string.chargingfull);
	               
	               break;   
	           
			}
			tvCharging.setText(getResources().getString(R.string.charging)+"    "+id);
			
			String healthString = "";   
			switch (health) {   
			case BatteryManager.BATTERY_HEALTH_UNKNOWN:   
               healthString = getResources().getString(R.string.nstatus);   
               break;   
			case BatteryManager.BATTERY_HEALTH_GOOD:   
               healthString = getResources().getString(R.string.ystatus);   
               break;   
			case BatteryManager.BATTERY_HEALTH_OVERHEAT:   
               healthString = getResources().getString(R.string.overheat);   
               break;   
			case BatteryManager.BATTERY_HEALTH_DEAD:   
               healthString = getResources().getString(R.string.dead);   
               break;   
			case BatteryManager.BATTERY_HEALTH_OVER_VOLTAGE:   
               healthString = getResources().getString(R.string.overvoltage);   
               break;   
			case BatteryManager.BATTERY_HEALTH_UNSPECIFIED_FAILURE:   
               healthString = getResources().getString(R.string.nstatus);   
               break;   
            }
			Status.setText(getResources().getString(R.string.status)+"   "+healthString);
			
			quantity.setText(getResources().getString(R.string.quantity)+"   "+current+"%");
			bvoltage.setText(getResources().getString(R.string.bvoltage)+"   "+voltage+"mV");
			//temp.setText(getResources().getString(R.string.temp)+"    "+temperature+"��C");
			
		}
	}

   @Override
    protected void onDestroy() {
    	// TODO Auto-generated method stub
    	super.onDestroy();
    	if(mWakeLock!=null)
    	{
        	mWakeLock.release();
        	mWakeLock =null;
    	}
    	if(receiver!=null)
    	{
    		unregisterReceiver(receiver);
			receiver=null;
    	}
		
		handler.removeCallbacks(updateCurrent);
    }

	   
	@Override
	protected void onStop() {
		// TODO Auto-generated method stub
        finish();
		super.onStop();
	}
	
	@Override
	public void onAttachedToWindow() 
	{
		// TODO Auto-generated method stub
		super.onAttachedToWindow();
		this.getWindow().setType(WindowManager.LayoutParams.TYPE_KEYGUARD);
	}

	 @Override
	 public boolean onKeyDown(int keyCode, KeyEvent event) {
		 	

		 	if(keyCode == KeyEvent.KEYCODE_BACK)
		 	{
				finish();
		 	}	 
		 	return true;
	 	}		 
	 //xiongxi add 2012-04-09
	    @Override
	    protected void onResume() {
	        // TODO Auto-generated method stub
	        super.onResume();
	        handler.post(updateCurrent);
	    }

	    private String getCurrent(String path) {
	        String current = "";
	        FileInputStream inputStream = null;
	        try {
	            inputStream = new FileInputStream(path);
	            int length = inputStream.available();
	            byte[] buffer = new byte[length];
	            inputStream.read(buffer);
	            current = EncodingUtils.getString(buffer, "UTF-8");
	        } catch (Exception e) {
	            e.printStackTrace();
	        } finally {
	            try {
	            	if(null != inputStream)
	            	{
	            		 inputStream.close();
	            	}
	               
	            } catch (IOException e) {
	                // TODO Auto-generated catch block
	                e.printStackTrace();
	            }

	        }
	        if(current == null || current.equals(""))
	        {
	        	current="0";
	        }
	        return current;
	    }
	    //end add
}
		 